package huu.phong.googlemusic;

import huu.phong.googlemusic.model.Song;

import java.awt.Point;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

public class Configure implements Serializable{
	private static final long serialVersionUID = 96678343791424943L;
	public List<Song> songs;
	public List<Song> allSong;
	public String title = "Google Music v1.0.1";
	public String host, port, username, password, defaultMediaPlayer = title;
	public boolean useProxy, systemProxy;
	public List<String> mediaPlayers;
	public int buffered = 5000;
	public String repeat = "Default";
	public String value = "";
	public Point location = null;
	public int numberReconnect = 5;
	public String status = title + " | huuphongdn2009@gmail.com";
	public String oldFolder = ".";
	public int timeLive = 7200000;
	public String googleUsername = "";
	public String googlePassword = "";
	public boolean autoLogin = true;
	public String sort = "Album";
	
	public Configure(){
		mediaPlayers = new ArrayList<String>();
		mediaPlayers.add(title);
		mediaPlayers.add("C:\\Program Files (x86)\\foobar2000\\foobar2000.exe");
		mediaPlayers.add("D:\\Software\\Portable\\VLC\\vlc.exe");
		mediaPlayers.add("C:\\Program Files\\Windows Media Player\\wmplayer.exe");
		mediaPlayers.add("C:\\Program Files (x86)\\Windows Media Player\\wmplayer.exe");
		mediaPlayers.add("/usr/bin/audacious");
		songs = new ArrayList<Song>();
	}
	
	public void save(){
		try {
			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Google Music.conf"));
			out.writeObject(this);
			out.flush();
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static Configure load(){
		Configure that = new Configure();
		File file = new File("Google Music.conf");
		if (!file.exists()) return that;
		try {
			ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
			Object object = in.readObject();
			if (object instanceof Configure){
				Configure exist = (Configure) object;
				if (exist.title != null && exist.title.equals(that.title)){
					that = exist;
				}else{
					JOptionPane.showMessageDialog(null, "File old configure of " + exist.title +  " is not compatible with " + that.title + "\r\nOld configure will rename to " + exist.title + ".old.conf", "Loading configuare", JOptionPane.INFORMATION_MESSAGE);
					boolean rename = false;
					try {
						ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(exist.title + ".old.conf"));
						out.writeObject(exist);
						out.flush();
						out.close();
						rename = true;
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
					JOptionPane.showMessageDialog(null, "Rename is " + (rename ? "Successfull" : "Fail") + "\r\nOld configuare will replace with default configuare of " + that.title, "Loading configuare", JOptionPane.INFORMATION_MESSAGE);
				}
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return that;
	}
}
